package com.tangzx.builder.visitor;

import java.util.LinkedList;

public class Scope implements IScope {

	private LinkedList<Object> stack = new LinkedList<Object>();
	private LinkedList<IScope> scopeList = new LinkedList<IScope>();
	private IScope parent;
	
	public Object value;
	
	public Scope(Object value)
	{
		this.value = value;
	}
	
	public Scope()
	{
		this("SCOPE.....");
	}
	
	public void push(Object item) {
		System.out.println("\t\t\tpush:" + item);
		stack.push(item);
	}

	public void pushScope(IScope scope) {
		scope.setParent(this);
		scopeList.push(scope);
	}

	public Object pop() {
		Object item = stack.pop();
		//System.out.println("Pop:" + item);
		return item;
	}

	public Object peek() {
		return null;
	}

	public IScope getLocal(int index) {
		return scopeList.get(index);
	}

	public IScope popScope() {
		return scopeList.pop();
	}

	public IScope getParent() {
		return parent;
	}

	public void setParent(IScope scope) {
		parent = scope;
	}

	public Object getValue() {
		return value;
	}

	@Override
	public String toString() {
		if (value != null) return value.toString();
		return "Scope is NULL.";
	}
}
